using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class BurinSkill5Area : TriggerSkillCollider
{
    HashSet<GameObject> statues = new();

    public override void Initialize(SkillData data, float radius, bool isShow = true)
    {
        base.Initialize(data, radius, isShow);

        transform.position = owner.transform.position;
        transform.localScale = new Vector3(radius * 0.2f, radius * 0.2f, 1);
        StartCoroutine(ThisSkill(data));
    }

    private IEnumerator ThisSkill(SkillData info)
    {
        int cout = 8;

        float damage = info.Damage;

        new Buff_Petrify(cout).Apply(owner, owner);
        new Buff_Protection(1, cout).Apply(owner, owner);

        yield return new WaitForSeconds(0.5f);

        FinalDamageInfo backInfo = DamageCalculator.CalculateDamage(info, owner);
        backInfo.canAvoid = false;

        while (cout-- > 0)
        {
            foreach (GameObject obj in targets.ToList())
            {
                if(obj.TryGetComponent(out Damageable tar)){
                    info.Damage = damage + statues.Count;
                    owner.ApplyDamage(tar, info, canFeedback);
                    new Buff_Stun(0.5f + 0.1f * cout).Apply(owner, tar);
                }
            }
  
            if(canFeedback) owner.TakeDamage(backInfo, false);

            yield return new WaitForSeconds(1f);
        }
        Destroy();
    }

    protected override void OnTriggerStay2D(Collider2D collision)
    {
        base.OnTriggerStay2D(collision);
        if (collision.GetComponent<BurinSkill1Statue>()||
            collision.GetComponent<BurinSkill4Statue>())
        {
            statues.Add(collision.gameObject);
        }
    }

    protected override void OnTriggerExit2D(Collider2D collision)
    {
        base.OnTriggerExit2D(collision);
        if (collision.GetComponent<BurinSkill1Statue>() ||
            collision.GetComponent<BurinSkill4Statue>())
        {
            statues.Remove(collision.gameObject);
        }
    }
}